package com.ztsoft.user.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ztsoft.user.pojo.enums.ResultCode;

import java.io.Serializable;

public class Result<T> implements Serializable {

    private Integer code;

    private String msg;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 其他操作状态
     *
     * @param msg  消息
     * @param code 状态码
     * @return 通用数据传输对象
     */
    public static <T> Result<T> returnMsg(String msg, int code) {
        Result<T> result = new Result<>();
        result.setMsg(msg);
        result.setCode(code);
        return result;
    }

    /**
     * 其他操作状态,带结果
     *
     * @param msg  消息
     * @param code 状态码
     * @param data 返回对象
     * @param <T>  返回对象类型
     * @return 其他操作状态, 带结果
     */
    public static <T> Result<T> returnDataMsg(String msg, int code, T data) {
        Result<T> result = new Result<>();
        result.setMsg(msg);
        result.setCode(code);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setCode(ResultCode.SUCCESS.getCode());
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> fail() {
        Result<T> result = new Result<>();
        result.setMsg(ResultCode.FAILED.getMsg());
        result.setCode(ResultCode.FAILED.getCode());
        return result;
    }

    /**
     * 系统异常
     */
    public static <T> Result<T> error() {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.ERROR.getCode());
        result.setMsg(ResultCode.ERROR.getMsg());
        return result;
    }

}